package util;

import static core.SeleneseTestCaseEx.u;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.*;
import java.util.TimeZone;


public class Util {
	private String testEmail = "";
	
	
	public String empVl(String inpString) {
		if (inpString.contentEquals("")) {
			return "[EMPTY]";
		} else {
			return inpString;
		}
	}
	
	public String getTimeNow() {
		Date now = new Date();
		return dataTime2String(now, "HH:mm:ss");
	}
	
	public String getDateNow() {
		Date now = new Date();
		return dataTime2String(now, "dd-MMM-yyyy");
	}
	
	public String getDateTimeNow() {
		Date now = new Date();
		return dataTime2String(now, "dd-MMM-yyyy, HH:mm:ss");
	}
	
	public String getTime(Date time) {
		return dataTime2String(time, "HH:mm:ss");
	}
	
	public String getTimeZone() {
		TimeZone tz = TimeZone.getDefault();
		Integer i = 0;
		
		i = tz.getRawOffset() / 1000 / 60 / 60; //convert msec to hours
		return ((i >=0) ? ("GMT+" + i + ".00") : ("GMT-" + i + ".00"));
	}	
	
	public String dataTime2String(Date date, String mask) {
		 SimpleDateFormat format = new SimpleDateFormat(mask);
		 return format.format(date);
	}

    /**Encodes specified string into HTML format by replacing special characters (&amp;, &gt;, &lt;, &quot;, SPACE, LINE BREAK, "\n")
     * with appropriate &amp;; codes. If string contains html-formatting then no encoding will be done.
     * @param strToEncode Specifies the string for encoding.
     * @return Encoded into HTML format string
     */
    public String encodeHTML(String strToEncode) {
    	Pattern ptrn = null;
        String[] patternsArr = {"b", "p", "font", "i", "table", "h1", "h2", "h3", "h4",  "Div"};
        
        boolean res;
        res = false;
        
        int k;
        String expression;
        String tagPattern;
        
        String result = "";
        for(k = 0; k < 10; k++) {
            tagPattern = patternsArr[k];
            expression = "<\\s*" + tagPattern + ".*\\n*>.*\\n*<\\s*/" + tagPattern + "\\s*>";
            ptrn = Pattern.compile(expression);
            res = ptrn.matcher(strToEncode).matches();

            if (res) {
            	result = strToEncode;
            } else {
            	strToEncode = strToEncode.replace("&", "&amp;");
            	strToEncode = strToEncode.replace(">", "&gt;");
            	strToEncode = strToEncode.replace("<", "&lt;");
            	strToEncode = strToEncode.replace("\"\"", "&quot;");
            	strToEncode = strToEncode.replace("  ", "&nbsp;&nbsp;");
            	strToEncode = strToEncode.replace("\\n", "<BR>");
            	result = strToEncode;
            }
        }		
        return result;          
    }
    
	/**
	 * Function create new trivial (original) Email
	 */
	public String getNewEmail(Boolean isNew) {
		if (isNew) {
			Date now = new Date();
			String newOriginEmail;
			newOriginEmail = "test" + u.dataTime2String(now, "ddMMyyyyHHmmss")+ "@mail.com";
			testEmail = newOriginEmail;
			return newOriginEmail;
		} else {
			return (testEmail == null ? getNewEmail(true) : testEmail);
		}
	}
}
	